package leetCode;

import java.util.*;

public class Q47 {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();

        int len = nums.length;
        if(len == 0) return res;
        Deque<Integer> path = new ArrayDeque<>();
        boolean[] used = new boolean[len];
        Arrays.sort(nums);
        dfs(nums,len,0,path,used,res);
        return res;
    }

    private void dfs(int[] nums, int len, int depth, Deque<Integer> path, boolean[] used, List<List<Integer>> res) {
        // 往下走一步
        if(depth == len){
            res.add(new ArrayList<>(path));
            return;
        }
        for(int i=0;i<len;i++){
            if(used[i] ||(i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) continue;

            path.add(nums[i]);
            used[i] = true;
            dfs(nums,len,depth+1,path,used,res);
            path.removeLast();
            used[i] = false;
        }
    }

    public static void main(String[] args) {
        String s = "1";
        s.contains(null);
    }
}
